package com.wzt.lianfirecontrol.lecheng;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

import com.lechange.opensdk.media.LCOpenSDK_PlayWindow;
import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.bean.recode.home.jiankong.JianKongDeviceModel;
import com.wzt.lianfirecontrol.lecheng.business.Business;
import com.wzt.lianfirecontrol.lecheng.fragment.MediaPlayFragment;
// 乐橙播放器
public class PlayLeChengActivity extends BaseActivity implements MediaPlayFragment.BackHandlerInterface,
        ActivityCompat.OnRequestPermissionsResultCallback {

    private Bundle bundle;
    private final static String tag = "PlayLeChengActivity";
    private MediaPlayFragment mMediaPlayFragment;

    private View include_head_title;
    private ImageView iv_back;
    public TextView tv_title;

    private JianKongDeviceModel jianKongDeviceModel;

    private LCOpenSDK_PlayWindow mPlayWin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bundle = getIntent().getExtras();
        if (null != bundle){
            jianKongDeviceModel = (JianKongDeviceModel) bundle.getSerializable("JianKongDeviceModel");
        }
        if (null == jianKongDeviceModel){
            finish();
            return;
        }

        setContentView(R.layout.a_play_lecheng);
        setImmerseLayout(findViewById(R.id.include_head_title), findViewById(R.id.rl_head_content));
        iniHeadTitle();
        Business.getInstance().init("lc561ab37efab84513", "4db0fe512c4f4629ac879815b4865c", "openapi.lechange.cn:443");
        Business.getInstance().adminlogin(handler);

//        PermissionHelper.requestPermission(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO});
//        // 嵌入使用的帧
//        MediaPlayFragment mediaPlayFragment; // 引用的布局帧
//        Bundle b = new Bundle();
//        String resId; // 资源id号
//        mediaPlayFragment = new MediaPlayOnlineFragment();
//        resId = getIntent().getStringExtra("UUID");
//        b.putString("RESID", resId);
//        mediaPlayFragment.setArguments(b);
//        changeFragment(mediaPlayFragment, false);
    }

    private void iniHeadTitle() {
        include_head_title = findViewById(R.id.include_head_title);
        include_head_title.setBackground(getResources().getDrawable(R.drawable.shape_head_title_bg_frame));
        iv_back = findViewById(R.id.iv_back);
        iv_back.setImageResource(R.drawable.back_white);
        iv_back.setVisibility(View.VISIBLE);
        tv_title = findViewById(R.id.tv_title);
        tv_title.setTextColor(getResources().getColor(R.color.white));
        tv_title.setText("视频监控");

        findViewById(R.id.v_divdier_line).setVisibility(View.GONE);
    }

    /**
     *
     *      boolean) 描述：切换fragment
     */
    public void changeFragment(Fragment targetFragment, boolean isAddToStack) {
        if (isAddToStack) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frame_content, targetFragment)
                    .addToBackStack(null).commitAllowingStateLoss();
        } else {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frame_content, targetFragment)
                    .commitAllowingStateLoss();
        }

    }

    @Override
    public void onBackPressed() {
        if (mMediaPlayFragment == null || !mMediaPlayFragment.onBackPressed()) {
            Log.d(tag, "onBackPressed");
            super.onBackPressed();
        }
    }

    @Override
    public void setSelectedFragment(MediaPlayFragment backHandledFragment) {
        // TODO Auto-generated method stub
        this.mMediaPlayFragment = backHandledFragment;
    }

    // 横竖屏切换需要,隐藏标题栏
    public void toggleTitle(boolean isShow) {
        include_head_title.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String[] permissions, int[] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//		if (grantResults.length == 1
//				&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//
//		} else {
//			Toast.makeText(this, R.string.no_access_write_external_storage,
//					Toast.LENGTH_SHORT).show();
//		}

    }

    @Override
    protected void dealWithMyMsgs(Message msg) {
        super.dealWithMyMsgs(msg);
        switch (msg.what){
            case 0:
                mPlayWin = new LCOpenSDK_PlayWindow();
                Business.getInstance().setToken((String)msg.obj);
                mPlayWin.initPlayWindow(this, (ViewGroup) findViewById(R.id.frame_content), R.id.frame_content);
                mPlayWin.playRtspReal(Business.getInstance().getToken(),
                jianKongDeviceModel.getSerial(), "", Integer.parseInt(jianKongDeviceModel.getChannelNo()), 0, true);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mPlayWin){
            mPlayWin.stopRtspReal();
        }
    }
}
